<?php
class MenuTree
{
    var $security_role;
    var $menu_users = "{text: 'Manajemen User',
                                id: 'jun.UsersGrid',
                                leaf: true
                                },";
    var $security = "{text: 'Security Role',
                                id: 'jun.SecurityRolesGrid',
                                leaf: true
                                },";
    function __construct($id)
    {
        //$this->security_role_id = $id;
        $role = SecurityRoles::model()->findByPk($id);
        $this->security_role = explode(",", $role->sections);
    }
    function get_menu_master()
    {
        $menu = "";
        $menu .= in_array(107, $this->security_role) ? "{
                            text: 'Kode Rekening',
                            id: 'jun.ChartMasterGrid',
                            leaf: true
                        },
                        {
                            text: 'Kas dan Bank',
                            id: 'jun.BankGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(106, $this->security_role) ? "{
                            text: 'Karyawan',
                            id: 'jun.SalesmanGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(105, $this->security_role) ? "{
                            text: 'Barang',
                            id: 'jun.BarangGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(116, $this->security_role) ? "{
                            text: 'Jenis Botol',
                            id: 'jun.JenisBotolGrid',
                            leaf: true
                        },
                        {
                            text: 'Suplier',
                            id: 'jun.SupplierGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(104, $this->security_role) ? "{
                            text: 'Area',
                            id: 'jun.AreaGrid',
                            leaf: true
                        },
                        {
                            text: 'Konsumen',
                            id: 'jun.KonsumenGrid',
                            leaf: true
                        },
                        {
                            text: 'Pasar',
                            id: 'jun.PasarGrid',
                            leaf: true
                        }," : "";
        if ($menu != "") {
            $menu = "{text: 'Master',
                  expanded: false,
                  children:[
                  $menu
                        ]},";
        }
        return $menu;
    }
    function get_menu_transaksi()
    {
        $menu = "";
        $menu .= self::get_menu_penjualan();
        $menu .= self::get_menu_pembelian();
        $menu .= in_array(117, $this->security_role) ? "{
                text: 'Saldo Awal Rekening',
                id: 'jun.SaldoAwalWin',
                leaf: true
            },
            {text: 'Saldo Awal Barang',
                id: 'jun.SaldoAwalBarangWin',
                leaf: true
            }," : "";
        if ($menu != "") {
            $menu = "{text: 'Transaksi',
                  expanded: false,
                  children:[
                  $menu
                        ]},";
        }
        return $menu;
    }
    function get_menu_persediaan()
    {
        $menu = in_array(114, $this->security_role) ? "{
                    text: 'Persediaan',
                    expanded: false,
                    children:[{
                            text: 'Pengelolaan',
                            id: 'jun.KelolaStokGrid',
                            leaf: true
                        },
                        {
                            text: 'Selisih',
                            id: 'jun.SelisihStokGrid',
                            leaf: true
                        }
                        ]},
                        {text: 'Jurnal',
                  expanded: false,
                  children:[{
                            text: 'Jurnal Umum',
                            id: 'jun.JurnalUmum',
                            leaf: true
                        },
                        {
                            text: 'Generate Laba Rugi',
                            id: 'jun.GenerateLabaRugi',
                            leaf: true
                        }
                        ]}," : "";
        return $menu;
    }
    function get_menu_penjualan()
    {
        $menu = "";
        $menu .= in_array(100, $this->security_role) || in_array(102, $this->security_role) ||
             in_array(118, $this->security_role) ? "{
                            text: 'Penjualan',
                            id: 'jun.PenjualanGrid',
                            leaf: true
                        },":"";
        return $menu;
    }
    function get_menu_pembelian()
    {
        $menu = "";
        $menu .= in_array(108, $this->security_role) || in_array(110, $this->security_role) || in_array(119, $this->security_role)? "{
                            text: 'Pesanan',
                            id: 'jun.PoGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(108, $this->security_role) ? "
                        {
                            text: 'Retur Pembelian',
                            id: 'jun.ReturPembelianGrid',
                            leaf: true
                        }," : "";
        $menu .= in_array(110, $this->security_role) ? "
                        {
                            text: 'Nota Debet',
                            id: 'jun.NotaDebetGrid',
                            leaf: true
                        },
                        {
                            text: 'Pelunasan Utang',
                            id: 'jun.PelunasanUtangGrid',
                            leaf: true
                        }," : "";

        if ($menu != "") {
            $menu = "{text: 'Pembelian',
                  expanded: false,
                  children:[
                  $menu
                        ]},";
        }
        return $menu;
    }
    function get_menu_kas()
    {
        $menu = "";
        $menu .= in_array(113, $this->security_role) ? "{
                            text: 'Kas Masuk',
                            id: 'jun.KasGrid',
                            leaf: true
                        },{
                            text: 'Kas Keluar',
                            id: 'jun.KasGridOut',
                            leaf: true
                        },{
                            text: 'Mutasi Kas',
                            id: 'jun.TranferBankWin',
                            leaf: true
                        }," : "";
        $menu .= in_array(112, $this->security_role) ? "{
                            text: 'Kas Kecil Masuk ',
                            id: 'jun.KasGridKecil',
                            leaf: true
                        },{
                            text: 'Kas Kecil Keluar',
                            id: 'jun.KasGridOutKecil',
                            leaf: true
                        }," : "";
        if ($menu != "") {
            $menu = "{text: 'Kas',
                  expanded: false,
                  children:[
                  $menu
                        ]},";
        }
        return $menu;
    }
    function get_laporan()
    {
        $menu = "";
        $menu .= in_array(101, $this->security_role) ? "{
                            text: 'Penjualan',
                            id: 'jun.LapPenjualan',
                            leaf: true
                        },
                        {
                            text: 'Daftar Piutang',
                            id: 'jun.LapDaftarPiutang',
                            leaf: true
                        },
                        {
                            text: 'Piutang Per Salesman',
                            id: 'jun.LapPiutangSales',
                            leaf: true
                        }," : "";
        $menu .= in_array(103, $this->security_role) ? "{
                            text: 'Laba Kotor Salesman',
                            id: 'jun.ReportLabaKotorSales',
                            leaf: true
                        },
                        {
                            text: 'Biaya Per Salesman',
                            id: 'jun.LapBiayaSales',
                            leaf: true
                        },
                        {
                            text: 'Botol Per Salesman',
                            id: 'jun.LapBotolSales',
                            leaf: true
                        }," : "";
        $menu .= in_array(109, $this->security_role) ? "{
                            text: 'Barang Sedang Dipesan',
                            id: 'jun.ReportBarangSedangPesan',
                            leaf: true
                        },
                        {
                            text: 'Barang Mencapai Titik Minimal',
                            id: 'jun.ReportBarangHabis',
                            leaf: true
                        },
                        {
                            text: 'Pembelian',
                            id: 'jun.ReportPembelian',
                            leaf: true
                        }," : "";
        $menu .= in_array(110, $this->security_role) ? "
                        {
                            text: 'Setoran Kas',
                            id: 'jun.ReportSetorKasPeriode',
                            leaf: true
                        }," : "";
        $menu .= in_array(111, $this->security_role) ? "{
                            text: 'Nota Debet',
                            id: 'jun.ReportNotaDebet',
                            leaf: true
                        },
                        {
                            text: 'Utang',
                            id: 'jun.ReportUtang',
                            leaf: true
                        }," : "";
        $menu .= in_array(113, $this->security_role) ? "{
                            text: 'Mutasi Kas',
                            id: 'jun.ReportMutasiKas',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Jurnal Umum',
                            id: 'jun.ReportJurnalUmum',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Pengelolaan Persediaan',
                            id: 'jun.ReportKelolaStok',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Selisih Persediaan',
                            id: 'jun.ReportSelisihStok',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Laba Rugi',
                            id: 'jun.ReportLabaRugi',
                            leaf: true
                        },{
                            text: 'Neraca',
                            id: 'jun.Neraca',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'General Ledger',
                            id: 'jun.ReportGeneralLedger',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Kartu Stok',
                            id: 'jun.ReportInventoryCard',
                            leaf: true
                        }," : "";
        $menu .= in_array(114, $this->security_role) ? "{
                            text: 'Closing',
                            id: 'jun.ClosingYear',
                            leaf: true
                        }," : "";
        if ($menu != "") {
            $menu = "{text: 'Laporan',
                  expanded: false,
                  children:[
                  $menu
                        ]},";
        }
        return $menu;
    }
    function get_menu_general()
    {
        $menu = "{text: 'Administrasi',
                  expanded: false,
                  children:[                  
                  $this->menu_users
                  $this->security
                  ]},";
        return $menu;
    }
    public function get_menu()
    {
        $username = Yii::app()->user->name;
        $data = "[";
        $data .= $this->get_menu_master();
        $data .= $this->get_menu_transaksi();
        $data .= $this->get_menu_persediaan();
        $data .= $this->get_menu_kas();
        $data .= $this->get_laporan();
        $data .= in_array(115, $this->security_role) ? $this->get_menu_general() : '';
        $data .= "{
                text: 'Ganti Password',
                id: 'jun.PasswordWin',
                leaf: true
              },
              {
                text: 'Logout ($username)',
                id: 'logout',
                leaf: true
              }]";
        return $data;
    }
}
